#include "threadPool.h"
char absolutePath[256]; // 绝对路径
char buf1[4096];
char buf2[4096];
char cmd[256];
char userName[256] = {0};
int id = 1;

// 判断userName是否已经在userinfo表里了
int isexistUser(char* userName){
    MYSQL* mysql = mysql_init(NULL);
    MYSQL* ret = mysql_real_connect(mysql, "localhost", "root", "123", "netdisk", 0, NULL, 0);
    if(ret == NULL){
        fprintf(stderr, "mysql real connect:%s\n", mysql_error(mysql));
        return(-1);
    }
    char sql[1024] = "select * from userinfo;";
    int qret = mysql_query(mysql, sql);
    if(qret != 0){
        fprintf(stderr, "mysql_query:%s\n", mysql_error(mysql));
        return -1;
    }
    MYSQL_RES* res = mysql_store_result(mysql);
    while(1){
        MYSQL_ROW row = mysql_fetch_row(res); //取出一行
        if(row == NULL){
            return 0;
        }
        unsigned n = 1; // 比较每一行第二列的元素
        if
    }
}
// 注册
int loginUsersql(char* userName){
    MYSQL* mysql = mysql_init(NULL);
    MYSQL* ret = mysql_real_connect(mysql, "localhost", "root", "123", "netdisk", 0, NULL, 0); // use netdisk
    if(ret == NULL){
        fprintf(stderr, "mysql real connect:%s\n", mysql_error(mysql));
        return(-1);
    }
    char sql[1024] = {0};
    sprintf(sql, "insert into userinfo(username) values('%s');", userName);
    printf("%s\n", sql);
    int qret = mysql_query(mysql, sql);
    if(qret != 0){
        fprintf(stderr, "mysql_query:%s\n", mysql_error(mysql));
        return -1;
    }
    mysql_close(mysql);
    return 0;
}
// mkdir
int mkdirsql(char* dir, char* user){
    user = userName;
}
void *threadFunc(void *arg)
{
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    while (1)
    {
        // 接受任务
        pthread_mutex_lock(&pthreadPool->taskQueue.mutex);
        while (pthreadPool->taskQueue.queueSize <= 0)
        {
            pthread_cond_wait(&pthreadPool->taskQueue.cond, &pthreadPool->taskQueue.mutex);
        }
        int netfd = pthreadPool->taskQueue.pFront->netfd;
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);
        // 完成任务
        
        
        recv(netfd, buf1, sizeof(buf1), 0);
        
        for (int i = 0; buf1[i] != ' '; i++)
        {
            cmd[i] = buf1[i];
        }
        char cd[] = "cd";
        char ls[] = "ls";
        char pwd[] = "pwd";
        char mkdir[] = "mkdir";
        char remove[] = "remove";
        char puts[] = "puts";
        char gets[] = "gets";
        int flag_cd = strcmp(cmd, cd);
        int flag_ls = strcmp(cmd, ls);
        int flag_pwd = strcmp(cmd, pwd);
        int flag_mkdir = strcmp(cmd, mkdir);
        int flag_remove = strcmp(cmd, remove);
        int flag_puts = strcmp(cmd, puts);
        int flag_gets = strcmp(cmd, gets);
        
        
        // cd
        if (flag_cd == 0)
        {

            send(netfd, absolutePath, sizeof(absolutePath), 0);
        }
        // ls
        else if (flag_ls == 0)
        {
           
        }
        // mkdir
        else if (flag_mkdir == 0)
        {
            int pre_id = id;
            // mkdirsql(char* dir, char* user) // user直接填userName

        }
        // rmdir
        else if(flag_remove == 0)
        {

        }
        // puts
        else if (flag_puts == 0)
        {
            
        }
        // gets
        else if (flag_gets == 0)
        {
            
        }
        // pwd
        else if (flag_pwd == 0)
        {
          
        }
        // 添加第一个username
        else
        {
            int pre_id = -1;
            strcpy(userName, buf1); 
            // 判断userName在不在userinfo的username项里
            // 如果在
            // printf("welcome");
            if(isexistUser(userName)){
                printf("welcome!\n");
            }
            // 如果不在
            else{
                loginUsersql(userName);
            }
            // dir
        }
    }
}
int makeWorker(threadPool_t *pthreadPool)
{
    for (int i = 0; i < pthreadPool->workerNum; i++)
    {
        pthread_create(&pthreadPool->tidArr[i], NULL, threadFunc, pthreadPool);
    }
    return 0;
}